package pt.mauromad.guias.controller.admin;

import static pt.mauromad.guias.utils.transformation.Transformations.Company2Dao;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pt.mauromad.guias.bean.CompanyBean;
import pt.mauromad.guias.controller.BaseController;
import pt.mauromad.guias.dao.CompanyDao;
import pt.mauromad.guias.dao.DomainDao;
import pt.mauromad.guias.utils.form.Action;

@Controller
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
@RequestMapping("/admin/account/{domain}/company")
public class AdminCompanyController extends BaseController {
	@RequestMapping(value = "/all")
	public String all(Model model, @PathVariable DomainDao domain) {
		model.addAttribute("companies", companyService.getByAccount(domain.getAccount().getId()));
		return "list/all-companies";
	}

	@RequestMapping(value = "/{company}", method = RequestMethod.GET)
	public String get(Model model, @PathVariable CompanyDao company, @PathVariable DomainDao domain) {
		if (company.getAccount().getDomain() != domain)
			return null;

		model.addAttribute("company", company);
		add(model, Action.createPost("manage"));
		add(model, Action.createPost("address.new"));
		return "company-form";
	}

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String create(Model model) {
		setEditable(model);
		add(model, Action.createPost("save.new"));
		return "company-form";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST, params = "action=save.new")
	public String createCompany(Model model, @Validated CompanyBean bean, @PathVariable DomainDao domain) {

		CompanyDao company = Company2Dao.get(bean);
		company.setAccount(domain.getAccount());
		companyService.save(company);

		return redirect("%s", domain, company.getId());
	}

	@RequestMapping(value = "/{company}", method = RequestMethod.POST, params = "action=manage")
	public String manage(Model model, @PathVariable CompanyDao company, @PathVariable DomainDao domain) {
		if (company.getAccount().getDomain() != domain)
			return null;

		setEditable(model);
		model.addAttribute("company", company);
		add(model, Action.createPost("manage.save"));
		return "company-form";
	}

	@RequestMapping(value = "/{company}", method = RequestMethod.POST, params = "action=manage.save")
	public String manageSave(Model model, @Validated CompanyBean company) {
		CompanyDao dao = Company2Dao.get(company);
		companyService.save(dao);
		return redirect("%s", company.getId());
	}

	@RequestMapping(value = "/{company}", method = RequestMethod.POST, params = "action=address.new")
	public String newAddress(Model model, @PathVariable CompanyDao company) {
		return redirect("%s/address/new", company.getId());
	}
}
